import Vue from "vue"
import Router from "vue-router"

Vue.use(Router)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

// 路由懒加载导入组件
// 一级路由
const Index = () => import("../pages/Index/Index.vue");
const Login = () => import("../pages/Login/Login.vue");
const Regist = () => import("../pages/Regist/Regist.vue");
const goodsList = () => import("../pages/goodsList/goodsList.vue");
const Details = () => import("../pages/Details/Details.vue");
const goodsSearch = () => import("../pages/goodsSearch/goodsSearch.vue");
const emptyList = () => import("../pages/Empty/emptyList.vue");
const emptySearch = () => import("../pages/Empty/emptySearch.vue");
const Order = () => import("../pages/Order/Order.vue");
const Address = () => import("../pages/Address/Address.vue");
const AddressAdd = () => import("../pages/Address/AddressAdd.vue");
const Pay = () => import("../pages/Pay/Pay.vue");

// 二级路由
const Home = () => import("../views/Home/Home.vue");
const Cate = () => import("../views/Cate/Cate.vue");
const Cart = () => import("../views/Cart/Cart.vue");
const Mine = () => import("../views/Mine/Mine.vue");
let router = new Router({
    routes: [
        {
            path: "/index",
            meta: {
                title: '小U商城'
            },
            components: {
                default: Index,
            },
            children: [
                {
                    path: "home",
                    component: Home,
                    meta: {
                        title: '小U商城'
                    }
                },
                {
                    path: "cate",
                    component: Cate,
                    meta:{
                        title:"商品分类"
                    }
                },
                {
                    path: "cart",
                    component: Cart,
                    meta:{
                        title:"购物车"
                    }
                },
                {
                    path: "mine",
                    component: Mine,
                    meta:{
                        title:"会员中心"
                    }
                },
                {
                    path: "/index",
                    meta: {
                        title: '小U商城'
                    },
                    redirect: "/index/home"
                }
            ]
        },
        {
            path: "/login",
            component: Login,
            meta:{
                title:"登录"
            }
        },
        {
            path: "/goodsList",
            component: goodsList,
            meta:{
                title:"商品列表"
            }
        },
        {
            path: "/goodsSearch",
            component: goodsSearch,
            meta:{
                title:"商品搜索"
            }
        },
        {
            path: "/details/:id",
            component: Details,
            meta:{
                title:"商品详情"
            }
        },
        {
            path: "/regist",
            component: Regist,
            meta:{
                title:"注册"
            }
        },
        {
            path: "/mine",
            component: Mine,
            meta:{
                title:"我的"
            }
        },
        {
            path: "/emptyList",
            component: emptyList,
            meta:{
                title:"商品空"
            }
        },
        {
            path: "/emptySearch",
            component: emptySearch,
            meta:{
                title:"搜索空"
            }
        },
        {
            path: "/order",
            component: Order,
            meta:{
                title:"订单支付"
            }
        },
        {
            path: "/address",
            component: Address,
            meta:{
                title:"地址列表"
            }
        },
        {
            path: "/addressAdd",
            component: AddressAdd ,
            meta:{
                title:"地址添加和修改"
            }
        },
        {
            path: "/pay",
            component: Pay,
            meta:{
                title:"支付页面"
            }
        },
        {
            path: "*",
            redirect: "/login",
            meta: {
                title: '登录'
            },
        }
    ],
    // 滚动行为处理
    /**
     * 滚动行为
     * 接收三个参数
     * to:要去往的路由对象
     * from:从哪个路由对象来
     * savePosition:当点击浏览器前进或者后退的时候才会起作用
     */
    scrollBehavior(to,from,savedPosition){
        // savedPosition当点击浏览器的前进或者后退的时候才会起作用
        if(savedPosition){
            return savedPosition
        }else{
            return {x:0,y:0}
        }
    }
})
// 全局路由守卫
// router.beforeEach((to,from,next)=>{
//     // console.log('beforeEach');
//     if(to.path !== '/login'){
//         if(sessionStorage.getItem('user')){
//             next()
//         }else{
//             next('/login')
//         }
//     }else{
//         next()
//     }
//     // to:要去往的路由对象
//     // console.log(to);
//     // from:从哪个路由对象过来
//     // console.log(from);
//     // 允许进入
//     // next()
// })
export default router